﻿using System;
using System.Collections.Generic;
using ZyGames.DZPocker.Model;
using ZyGames.Framework.Cache.Generic;
using ZyGames.Framework.Game.Cache;
using ZyGames.Framework.Game.Context;
using ZyGames.Framework.Game.Contract;
using ZyGames.Framework.Game.Contract.Action;
using ZyGames.Framework.Game.Lang;
using ZyGames.Framework.Game.Service;
using System.Linq;
using ZyGames.DZPocker.Bll.Logic;
using ZyGames.DZPocker.Script;
using ZyGames.Framework.Net;
using ZyGames.Framework.RPC.Sockets;
using ZyGames.DZPocker.Tools;

namespace ZyGames.DZPocker.Script.CsScript.Action
{
	/// <summary>
	/// 玩家点击加入牌局进入准备状态
	/// </summary>
	public class Action20015 : BaseAction
	{
		private int _tableId;
		private int _userId;
		private int _posId;
		private bool _result = false;
		private GameUser _currentGameUser;
		private TableData _currentTable;

		public Action20015(HttpGet actionGetter)
			: base((short)ActionType.DZPlayerGetReady, actionGetter)
		{
		}
		public override bool GetUrlElement()
		{
			if (httpGet.GetInt("TableID", ref _tableId) &&
				httpGet.GetInt("UserID", ref _userId) &&
				httpGet.GetInt("PosID", ref _posId))
			{
				return true;
			}
			return false;
		}

		public override bool TakeAction()
		{
			_currentTable = GameRoom.Current.GetTableData(_tableId);
			if (_currentTable == null)
			{
				Tool.WriteWarmingLine("消息20015获得房间不存在");
				return false;
			}
			if (_posId > _currentTable.PlayerNum)
			{
				Tool.WriteWarmingLine("消息20015座位号不合法");
				return false;
			}
			var pos = _currentTable.Positions[_posId - 1];
			if (pos == null)
			{
				Tool.WriteWarmingLine("消息20015座位号不存在");
				return false;
			}
			_currentGameUser = GameTable.Current.GetUser(_userId);
			if (_currentGameUser == null)
			{
				Tool.WriteWarmingLine("消息20015玩家不存在");
				return false;
			}
			//如果当前位置没有准备，就设置玩家的属性
			if (!pos.Ready())
				_currentGameUser.Property.SetReadyState(_tableId, true);
				
			_result = true;

			return true;
		}

		public override void BuildPacket()
		{
			this.PushIntoStack(_result);
		}
	}
}
